import Vue from "vue";
import VueRouter from "vue-router";


Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/home"
  },
  {
    path: "/home",
    name: "home",
    component: () => import("../views/Home.vue")
  },
  {
    path: "/fenlei",
    name: "fenlei",
    component: () => import("../views/Category.vue")
  },
  {
    path: "/cart",
    name: "cart",
    meta: { //组件的额外配置项
      keepAlive: true,
      loginStatus: true
    },
    component: () => import("../views/Cart.vue"),
    // 路由配置里的钩子函数
    // beforeEnter: (to, from, next) => {
    //   // ...
    //   if (from.name == "my") {
    //     if (localStorage.token) {
    //       next()
    //     } else {
    //       next("/login")
    //     }
    //   }
    // }
  },
  {
    path: "/my",
    name: "my",
    component: () => import("../views/My.vue")
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../components/Login.vue")
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});

export default router;
